package com.xms.consumers.security;

import com.xms.core.module.ModuleCollection;
import com.xms.event.core.AuthorizationStateChangedEvent;
import com.xms.security.service.dataauth.IRoleObjectAccessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 权限资源状态更改事件处理
 * </p>
 *
 * @author migo
 * @since 2021/10/8
 */
@Service
public class AuthorizationStateChanged {
    private IRoleObjectAccessService _roleObjectAccessService;

    @Autowired
    public AuthorizationStateChanged(IRoleObjectAccessService roleObjectAccessService) {
        _roleObjectAccessService = roleObjectAccessService;
    }

    @Async
    @EventListener
    public void handleEvent(AuthorizationStateChangedEvent eventMessage) {
        if (!eventMessage.state) {
            _roleObjectAccessService.deleteByObjectId(ModuleCollection.getIdentity(eventMessage.resourceName), eventMessage.objectId);
        }
    }
}